package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo5 {
    public static void main(String[] args) {
        try (
                Connection connection = DBUtil.getConnection();
        ){
            /*
                查看王克晶所带班级信息:老师名字，性别，工资，班级名字，所在楼层
                SELECT t.name,t.gender,t.salary,c.name,c.floor
                FROM teacher t,class c
                WHERE t.id=c.teacher_id
                AND t.name='王克晶'
             */
            Statement statement = connection.createStatement();
            String sql = "SELECT a.name, a.gender, a.salary, b.name, b.floor "+
                         "FROM teacher a, class b "+
                         "WHERE a.id = b.teacher_id "+
                         "and a.name = '王克晶'";
            ResultSet rs = statement.executeQuery(sql);
            while ((rs.next())) {
                String tName = rs.getString("a.name");
                String tGender = rs.getString("a.gender");
                int tSarary = rs.getInt("a.salary");
                String cName = rs.getString("b.name");
                int cFloor = rs.getInt("b.floor");

                System.out.println(
                        String.join(",",tName,tGender,tSarary+"",cName,cFloor+"")
                );

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
